﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using Module.Common.Models;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
    public class ProductOrderDAO
	{
		#region Fields

		protected string connectionStringName;

		#endregion
	
		#region Methods

		/// <summary>
		/// Saves a record to the ProductOrder table.
		/// </summary>
		public virtual int Insert(ProductOrderInfo productOrderInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CodeMember", productOrderInfo.CodeMember),
				new SqlParameter("@OrderDate", productOrderInfo.OrderDate),
				new SqlParameter("@TotalPrice", productOrderInfo.TotalPrice),
				new SqlParameter("@Notes", productOrderInfo.Notes),
				new SqlParameter("@Status", productOrderInfo.Status),
                new SqlParameter("@TotalDealxu", productOrderInfo.TotalDealxu)
			};

			return (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "ProductOrder_Insert", parameters);
		}        
		/// <summary>
		/// Updates a record in the ProductOrder table.
		/// </summary>
		public virtual void Update(ProductOrderInfo productOrderInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@OrderId", productOrderInfo.OrderId),
				new SqlParameter("@CodeMember", productOrderInfo.CodeMember),
				new SqlParameter("@OrderDate", productOrderInfo.OrderDate),
				new SqlParameter("@TotalPrice", productOrderInfo.TotalPrice),
				new SqlParameter("@Notes", productOrderInfo.Notes),
				new SqlParameter("@Status", productOrderInfo.Status),
                new SqlParameter("@TotalDealxu", productOrderInfo.TotalDealxu)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ProductOrder_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the ProductOrder table by its primary key.
		/// </summary>
		public virtual void Delete(string orderId,int param)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@OrderId", orderId),
                new SqlParameter("@Param", param)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ProductOrder_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the ProductOrder table.
		/// </summary>
        public virtual ProductOrderInfo Select(string codeMember)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CodeMember", codeMember)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ProductOrder_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeProductOrderInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

        public virtual ProductOrderInfo Selects(int type,string param)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Type", type),
                new SqlParameter("@Param", param)
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ProductOrder_Selects", parameters))
            {
                if (dataReader.Read())
                {
                    return MakeProductOrderInfo(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }

        public virtual int SumDealxu(int type, string param)
        {
            int tong = 0;
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Type", type),
				new SqlParameter("@Param", param)
			};
            DataTable dt = SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "ProductOrder_Selects", parameters);
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["TotalDealxu"].ToString() != null && dt.Rows[0]["TotalDealxu"].ToString() != "")
                    tong = int.Parse(dt.Rows[0]["TotalDealxu"].ToString());
            }
            else
            {
                tong = 0;
            }
            return tong;
        }
        //Tổng số bản ghi

        public virtual int SelectCount(int status, int codeMember, string starDatetime, string endDatetime)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{           
               new SqlParameter("@Status", status),
               new SqlParameter("@CodeMember", codeMember),
               new SqlParameter("@starDatetime", starDatetime),
               new SqlParameter("@endDatetime", endDatetime),
               new SqlParameter("@IsTotalRow", 1)
			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "ProductOrder_Search", parameters);
        }


        public virtual DataTable Search(int status, int codeMember, string starDatetime, string endDatetime, int beginRow, int endRow, string orderByColumn)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
                new SqlParameter("@Status", status),
                new SqlParameter("@CodeMember", codeMember),
                new SqlParameter("@starDatetime", starDatetime),
                new SqlParameter("@endDatetime", endDatetime),               
                new SqlParameter("@BeginRow", beginRow),
                new SqlParameter("@EndRow", endRow),
                new SqlParameter("@OrderByColumn", orderByColumn),
				new SqlParameter("@isTotalRow", 0)
			};

            return SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "ProductOrder_Search", parameters);
        }

        //View đơn hàng bên ngoài web
        public DataSet ListProductOrder(string codeMember,string email, int currPage, int recodperPage, int pageSize,string link)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {                                              
                                             
                                              new SqlParameter("@currPage",currPage),
                                              new SqlParameter("@recodperpage",recodperPage),
                                              new SqlParameter("@Pagesize",pageSize),
                                              new SqlParameter("@Email",email),
                                              new SqlParameter("@CodeMember",codeMember),
                                              new SqlParameter("@link",link)
                                           


                                             
                                          };
            using (DataSet ds = SqlClientUtility.ExecuteDataSet(connectionStringName, CommandType.StoredProcedure, "View_ProductOrder", parameters))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    return ds;
                return null;
            }
        }

		/// <summary>
		/// Selects all records from the ProductOrder table.
		/// </summary>
		public CHRTList<ProductOrderInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ProductOrder_SelectAll"))
			{
				CHRTList<ProductOrderInfo> productOrderInfoList = new CHRTList<ProductOrderInfo>();
				while (dataReader.Read())
				{
					ProductOrderInfo productOrderInfo = MakeProductOrderInfo(dataReader);
					productOrderInfoList.Add(productOrderInfo);
				}

				return productOrderInfoList;
			}
		}

        //Update lại trạng thái 
        public virtual void UpdateStatus(int orderId, int status)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@OrderId", orderId),
                new SqlParameter("@Status", status),
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ProductOrder_UpdateStatus", parameters);
        }

        //Bind dữ liệu vào Dropdownlist
        public DataSet BindDropdownlist(int orderId)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                              new SqlParameter("@OrderId", orderId)
                                            
                                          };
            using (DataSet ds = SqlClientUtility.ExecuteDataSet(connectionStringName, CommandType.StoredProcedure, "ProductOrder_BindDropDownList", parameters))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    return ds;
                return null;
            }
        }


        //Trừ tiền của khách hàng khi mua hàng

        public virtual void Member_UpdateGoldMuaHang(int type, string codeMember_from, float totalDealxu)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@type", type),
				new SqlParameter("@CodeMember_From", codeMember_from),
				new SqlParameter("@TotalPrice", totalDealxu)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Member_UpdateGoldMuaHang", parameters);
        }

		/// <summary>
		/// Creates a new instance of the ProductOrder class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual ProductOrderInfo MakeProductOrderInfo(SqlDataReader dataReader)
		{
			ProductOrderInfo productOrderInfo = new ProductOrderInfo();
			productOrderInfo.OrderId = SqlClientUtility.GetInt32(dataReader,DbConstants.PRODUCTORDER.ORDERID, 0);
            productOrderInfo.CodeMember = SqlClientUtility.GetString(dataReader, DbConstants.PRODUCTORDER.CODEMEMBER, String.Empty);
			productOrderInfo.OrderDate = SqlClientUtility.GetDateTime(dataReader,DbConstants.PRODUCTORDER.ORDERDATE, DateTime.Now);
		    productOrderInfo.TotalPrice = SqlClientUtility.GetFloat(dataReader,DbConstants.PRODUCTORDER.TOTALPRICE, 0);
			productOrderInfo.Notes = SqlClientUtility.GetString(dataReader,DbConstants.PRODUCTORDER.NOTES, String.Empty);
			productOrderInfo.Status = SqlClientUtility.GetInt32(dataReader,DbConstants.PRODUCTORDER.STATUS, 0);
            productOrderInfo.TotalDealxu = SqlClientUtility.GetFloat(dataReader, DbConstants.PRODUCTORDER.TOTALDEALXU, 0.0F);
			return productOrderInfo;
		}

		#endregion
	}
}
